package com.jnu.hospitalexamination.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jnu.hospitalexamination.common.Result;
import com.jnu.hospitalexamination.entity.Cidetailedreport;
import com.jnu.hospitalexamination.entity.Cireport;
import com.jnu.hospitalexamination.entity.Overallresult;
import com.jnu.hospitalexamination.entity.vo.CheckitemVO;
import com.jnu.hospitalexamination.entity.vo.ReportitemVO;
import com.jnu.hospitalexamination.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private CireportService cireportService;
    @Autowired
    private CidetailedreportService cidetailedreportService;
    @Autowired
    private OverallresultService OverallresultService;
    @Autowired
    private CheckitemService checkitemService;
    // 体检报告列表，返回ReportitemVO的列表
    @RequestMapping("/list")
    public Result getReportList(Integer userid) {
        List<ReportitemVO> reportlist = cireportService.getReportlist(userid);
        if (reportlist.isEmpty()){
            return Result.error(5001, "报告列表不存在", null);
        }
        return Result.success(200, "报告列表查询成功", reportlist);
    }
    // 体检报告详情
    @RequestMapping("/getReportDetails")
    public Result getReportDetails(Integer orderId, Integer ciId) {
        LambdaQueryWrapper<Cidetailedreport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cidetailedreport::getOrderid, orderId);
        List<Cidetailedreport> cidetailedreportlist = cidetailedreportService.list(queryWrapper);
        if (cidetailedreportlist.isEmpty()) {
            return Result.error(5002, "报告不存在", null);
        }
        return Result.success(200, "报告查询成功", cidetailedreportlist);
    }
    // 体检结论
    @RequestMapping("/Overallresult")
    public Result getOverallresult(Integer orderid) {
        LambdaQueryWrapper<Overallresult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Overallresult::getOrderid, orderid);
        List<Overallresult> overallresultlist = OverallresultService.list(queryWrapper);
        if (overallresultlist.isEmpty()) {
            return Result.error(5003, "总检不存在", null);
        }
        return Result.success(200, "总检查询成功", overallresultlist);
    }
    // 体检异常项目
    @RequestMapping("/Abnormal")
    public Result getAbnormal(Integer orderid) {
        LambdaQueryWrapper<Cidetailedreport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cidetailedreport::getOrderid, orderid).eq(Cidetailedreport::getIserror, 1);
        List<Cidetailedreport> errorlist = cidetailedreportService.list(queryWrapper);
        if (errorlist == null) {
            return Result.error(5004, "异常项目不存在", null);
        }
        return Result.success(200, "异常项目查询成功", errorlist);
    }
    @RequestMapping("/checkitem")
    public Result getCheckitem(Integer smid) {
        List<CheckitemVO> checkitem = checkitemService.getCheckitemBySmid(smid);
        if (checkitem.isEmpty()) {
            return Result.error(5005, "检查项目不存在", null);
        }
        return Result.success(200, "检查项目查询成功", checkitem);
    }

    /**
     * 添加总检结论
     * @param orderid
     * @param title
     * @param content
     * @return
     */
    @RequestMapping("/addOverallResult")
    @Transactional
    public Result addOverallresult(Integer orderid, String title ,String content) {
        Overallresult overallresult = new Overallresult();
        overallresult.setTitle(title);
        overallresult.setContent(content);
        overallresult.setOrderid(orderid);
        boolean save = OverallresultService.save(overallresult);
        if (!save) {
            return Result.error(500, "总检结论添加失败", null);
        }
        return Result.success(200, "总检结论添加成功", null);
    }

    /**
     * 根据orid删除总检结论
     * @param orid
     * @return
     */
    @RequestMapping("/deleteOverallResult")
    @Transactional
    public Result deleteOverallresult(Integer orid) {
        boolean remove = OverallresultService.removeById(orid);
        if (!remove) {
            return Result.error(500, "总检删除失败", null);
        }
        return Result.success(200, "总检删除成功", null);
    }

    /**
     * 更新总检结论
     * @param orid
     * @param title
     * @param content
     * @return
     */
    @RequestMapping("/updateOverallResult")
    @Transactional
    public Result updateOverallresult(Integer orid, String title ,String content) {
        Overallresult overallresult = new Overallresult();
        overallresult.setTitle(title);
        overallresult.setContent(content);
        overallresult.setOrid(orid);
        boolean save = OverallresultService.updateById(overallresult);
        if (!save) {
            return Result.error(500, "总检更新失败", null);
        }
        return Result.success(200, "总检更新成功", null);
    }
}
